/*!
 * 初始化.
 * ecp.qzz.init.js
 * 创建：QZZ
 * 日期：2014-03-01
 */ 
function scriptUtil() {
    this.scriptList = {};
	this.cssList = {};
	this.com = {};
};

scriptUtil.prototype = {

	scrType : function(scr) {
                var scrAry = scr.split(".");
		if (scrAry.length > 1) {
			return scrAry[scrAry.length - 1].toUpperCase();
		} else {
			return "";
		}
	},
	isImport : function(src) {
		return (this.scriptList[src] != null || this.cssList[src] != null);
	},
	LogInfo : function(src) {
		if(window.console && window.console.info) {
			console.info(src);
		}
	},
	getPath : function() {
		var srcUrl = document.getElementsByTagName("script")[0].src;
		var re = /[\/\\]+/g;
		if (/([a-zA-Z]:)|([hH][tT][tT][pP]:\/\/)/.test(srcUrl)) {
			var docUrl = document.URL;
			var srcAry = srcUrl.split(re);
			var docAry = docUrl.split(re);
			var indexs = 0;
			for ( var i = 0, len = srcAry.length < docAry.length ? srcAry.length
					: docAry.length; i < len; i++) {
				if (srcAry[i] !== docAry[i]) {
					indexs = i;
					break;
				}
			}
			var path = "";
			for ( var i = indexs, len = docAry.length; i < len - 1; i++) {
				path = path + "../";
			}
			for ( var i = indexs, len = srcAry.length; i < len - 1; i++) {
				path = path + srcAry[i] + "/";
			}
			return path;
		} else {
			var srcAry = srcUrl.split(re);
			var path = "";
			for ( var i = 0, len = srcAry.length; i < len - 1; i++) {
				path = path + srcAry[i] + "/";
			}
			return path;
		}
	},
	Import : function(src, callBack, sPath) {
        //路径
        if(sPath == null) sPath = "";
        //转换成数组
        var src = src || [];
        if(typeof src == "string") {
            src = [src];
        }
        var _doc = document.getElementsByTagName("head")[0];
        var importObj = {};
        if(src.length > 0) {
            var curSrc = sPath + src[0];
            src.splice(0, 1);
            var srctype = this.scrType(curSrc);
            if(this.scriptList[curSrc] == null 
                && this.cssList[curSrc] == null) {
		    	if ("JS" == srctype) {                        
                   	//导入js文件
				    importObj = document.createElement("script");
				    importObj.type = "text/javascript";
				    importObj.language = "javascript";
				    importObj.src = curSrc;                    
                    this.scriptList[curSrc] = 0;
		   	    } else if ("CSS" == srctype) {                        
                    //导入样式文件
				    importObj = document.createElement("link");
				    importObj.rel = "stylesheet";
				    importObj.type = "text/css";
				    importObj.href = curSrc;                    
                    this.cssList[curSrc] = 0;
                }
                importObj.csrc = curSrc;  
                importObj.cstype = srctype;
                importObj.self = this;
                //导入成功事件
		   	    importObj.onload = importObj.onreadystatechange = function() {
                    var csrc = this.csrc;                        
				    if(!this.readyState||this.readyState=='loaded'||this.readyState=='complete') {                                
                        var cst = this.cstype;
                        var Self = this.self;                                	
					    if ("JS" == cst) {
                            Self.scriptList[csrc] = "sucess"; 
                            Self.LogInfo("import script " + csrc + " sucess.");
                        } else if  ("CSS" == cst) {
                            Self.cssList[csrc] = "sucess";
                            Self.LogInfo("import css " + csrc + " sucess.");
                        }
                        this.onload=this.onreadystatechange=null;
                        if(src.length > 0) {
                            Self.Import(src, callBack, sPath);
                        } else if(typeof callBack == "function") {
                            callBack(true);
                        }
                        this.self = null;
                    }
		    	}
                //导入错误的事件
		    	importObj.onerror = function() {
                    var Self = this.self;
                    var csrc = this.csrc; 
                    var cst = this.cstype;								
                    if ("JS" == cst) {
                        Self.scriptList[csrc] = "error"; 
                        Self.LogInfo("import script " + csrc + " error.");                                         
                    } else if  ("CSS" == cst) {
                        Self.cssList[csrc] = "error";
                        Self.LogInfo("import css " + csrc + " error.");
                    }
                    _doc.removeChild(importObj);
                    this.onerror = null;
                    if(src.length > 0) {
                        Self.Import(src, callBack, sPath);
                    } else if(typeof callBack == "function") {
                        callBack(true);
                    }
                    this.self = null;
		   	    }
                _doc.appendChild(importObj);
		    } else {
                if(src.length > 0) {
                    this.Import(src, callBack, sPath);
                } else if(typeof callBack == "function") {
                    callBack(true);
                }
            }
        } else if(typeof callBack == "function") {
            callBack(true);
        }               
	}// Import
}
//win object
var thisWindow = new scriptUtil();

function define(deps, callBack) {
	if(typeof deps == "function" && callBack == null) {
		 callBack = deps;
		 deps = [];
	}
	var script = document.getElementsByTagName("script");
	var path = "";
	for(var i = 0, len = script.length; i < len; i++) {
		var src = script[i].src;
		if(src != null && src.indexOf("init.js") >= 0) {
			script = script[i];
			path = src.replace(/ecp.qzz.init.js/, "");
			break;
		}
	}
	thisWindow.Import(deps, function(state){
		if(state) {
			if(typeof callBack == "function") {
				callBack();
			}
		}
	}, path);
}

(function startUp() {
	//获取相对路径
	var script = document.getElementsByTagName("script");
	var path = "";
	for(var i = 0, len = script.length; i < len; i++) {
		var src = script[i].src;
		if(src != null && src.indexOf("init.js") >= 0) {
			script = script[i];
			path = src.replace(/ecp.qzz.init.js/, "");
			break;
		}
	}
	thisWindow.Import("ecp.qzz.config.js", function(state){
		//加载script列表
		thisWindow.Import(scriptFiles, function(state) {
			var cururl = script.getAttribute("data-main");
			if(cururl != null) {
				cururl = window.location.href.replace(/[^\/]+.html/,cururl + "_debug" + ".js");
			}
			thisWindow.Import(cururl, function(){
				if(typeof callBack == "function") {
					callBack(state);
				}
			});
		}, path);
	}, path);    
})();